<template>
	<div>
    <el-button type="primary" @click="handleAdd" style="float: right; margin-top: -55px;" size="mini"
    v-hasPermi="['labsafety:labsafetyForm:edit']">新建安全检查</el-button>
		<!-- 添加或修改安全检查单对话框 -->
    <el-dialog :title="title" center :visible.sync="open" width="720px" @close="close" append-to-body>
      <el-tabs v-model="activeName">
        <el-tab-pane label="流程表单" name="first">
          <AddTab @updateForm="updateForm" ref="addTab" :form="form"></AddTab>
        </el-tab-pane>
        <el-tab-pane v-if="form.instanceId" label="检查指标" name="second">
          <ItemTab ref="itemTab" :data="form.labSafetyCheckIndexList" :instanceId="form.instanceId"></ItemTab>
        </el-tab-pane>
        <el-tab-pane label="流程导入" name="third" v-if="form.instanceId">
          <FlowChoose ref="workFlow" @workflow="setWorkflow" :businessType="form.businessTypeOpt" :applyScope="form.applyScope"></FlowChoose>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="activeName=='first' || activeName=='second'"
        >保存</el-button>
        <el-button type="primary" @click="submitWorkflow" v-if="activeName=='third'"
        >提交</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>
	</div>
</template>
<script>
import FlowChoose from '@/views/labdata/flowDefination/flow_choose.vue'
import { getCheckedList } from "@/api/labdata/employee"
import { getCheckedList as getOrgCheckedList} from "@/api/labdata/orgnization"
import AddTab from "./addTab.vue"
import ItemTab from "./itemTab.vue"
import { formatCurrentDate } from "@/utils/index";
import { getLabsafetyForm, submitLabsafetyForm } from "@/api/labsafety/labsafetyForm";
export default {
  name: "CreateDialog",
  components: { AddTab, ItemTab, FlowChoose },
  data() {
    return {
      title: '南京邮电大学实验室安全检查',
      activeName: 'first',
      open: false,
      form: {},
    };
  },
  created() {
  },
  methods: {
    setWorkflow(workflowId){
      console.log('set form workflowId:'+workflowId);
      this.form.workflowId = workflowId;
    },
    submitWorkflow(){
        if(!this.form.workflowId){
          this.form.workflowId = this.$refs["workFlow"].getWorkflowId();
        }
        submitLabsafetyForm(this.form).then(response => {
          this.msgSuccess("提交成功");
          this.$emit("refresh");
          this.open = false;
        });
    },
    updateForm(form) {
      this.form = form
      getLabsafetyForm(form.instanceId).then(response => {
        this.form = response.data;
        this.$emit("refresh");
      })
      if(this.activeName=='first'){
        this.activeName = 'second';
      }else{
        this.activeName = 'third';
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.createBy = this.$store.state.user.name
      this.form.createTime = formatCurrentDate()
      this.activeName = 'first'
      this.open = true;
      if(this.$refs["addTab"]){
        this.$refs["addTab"].reset();
      }
    },
    /** 更新按钮操作 */
    handleUpdate(id) {
      this.reset()
      this.activeName = 'first'
      this.open = true;
      getLabsafetyForm(id).then(response => {
        this.form = response.data;
        if (this.form.copyToUsers) {
          getCheckedList({checkedUserIds: this.form.copyToUsers}).then(res=>{
            this.$refs["addTab"].copyToUsers = res.data
          });
        } else {
            this.$refs["addTab"].copyToUsers = []
        }
        if (this.form.checkUsers) {
          getCheckedList({checkedUserIds: this.form.checkUsers}).then(res=>{
            this.$refs["addTab"].checkUsers = res.data
          });
        } else {
            this.$refs["addTab"].checkUsers = []
        }
        if (this.form.reviewUsers) {
          getCheckedList({checkedUserIds: this.form.reviewUsers}).then(res=>{
            this.$refs["addTab"].reviewUsers = res.data
          });
        } else {
            this.$refs["addTab"].reviewUsers = []
        }
        if (this.form.checkTargetOrgs) {
          getOrgCheckedList({checkedOrgIds: this.form.checkTargetOrgs}).then(res=>{
            this.$refs["addTab"].checkTargetOrgs = res.data
          });
        } else {
            this.$refs["addTab"].checkTargetOrgs = []
        }
        this.$refs["addTab"].fileList = []
        if (this.form.files) {
            this.$refs["addTab"].fileList = this.form.files
        }
      })
    },
    close() {
      this.activeName = 'first'
      this.open = false
      //this.$refs["addTab"].reset()
    },
    reset() {
      this.form = {
        safetyTemplateOptArray:[],
        checkUsers:[],
        checkTargetOrgs:[],
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["addTab"].submitForm();
      // this.open = false;
    },
  }
};
</script>
<style type="text/css" scoped>
</style>
